/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ImageMap.java
 *
 * Created on 14/07/2010, 14:47:15
 */
package systole.view.tabs.segments;

import java.awt.GridLayout;
import java.awt.Image;
import java.util.Iterator;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;
import systole.view.tabs.JPnlSegments;
import systole.view.tabs.controllers.AnalysisController;
import systole.view.tabs.controllers.SelectionItem;
import systole.view.tabs.controllers.SelectionModel;

/**
 *
 * @author jmj
 */
public class JPnlImageMap extends javax.swing.JPanel implements Observer {

    /**
     *
     */
    private static final long serialVersionUID = -2416345491526492400L;
    private AnalysisController controller;
    private Vector<JPnlImg> components;
    private JPnlImg clicked = null;
    private JPnlSegments parent;

    /** Creates new form ImageMap 
     * @param controller 
     * @param parent 
     */
    public JPnlImageMap(AnalysisController controller, JPnlSegments parent) {
        initComponents();
        this.controller = controller;
        this.parent = parent;
        this.setFocusable(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPaneCenter = new javax.swing.JScrollPane();
        jPnlImages = new javax.swing.JPanel();
        jPnlTop = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPnlBottom = new javax.swing.JPanel();
        jPnlRigth = new javax.swing.JPanel();
        jPnlLeft = new javax.swing.JPanel();

        setName("Form"); // NOI18N
        setLayout(new java.awt.BorderLayout());

        jScrollPaneCenter.setName("jScrollPaneCenter"); // NOI18N

        jPnlImages.setMinimumSize(new java.awt.Dimension(500, 300));
        jPnlImages.setName("jPnlImages"); // NOI18N
        jPnlImages.setLayout(new java.awt.GridLayout(1, 4, 2, 2));
        jScrollPaneCenter.setViewportView(jPnlImages);

        add(jScrollPaneCenter, java.awt.BorderLayout.CENTER);

        jPnlTop.setMaximumSize(new java.awt.Dimension(32767, 10));
        jPnlTop.setMinimumSize(new java.awt.Dimension(0, 1));
        jPnlTop.setName("jPnlTop"); // NOI18N
        jPnlTop.setPreferredSize(new java.awt.Dimension(400, 25));
        jPnlTop.setLayout(new java.awt.BorderLayout());

        jLabel1.setFont(jLabel1.getFont().deriveFont(jLabel1.getFont().getStyle() | java.awt.Font.BOLD, jLabel1.getFont().getSize()+2));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(systole.view.SystoleApp.class).getContext().getResourceMap(JPnlImageMap.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        jPnlTop.add(jLabel1, java.awt.BorderLayout.CENTER);

        add(jPnlTop, java.awt.BorderLayout.PAGE_START);

        jPnlBottom.setMaximumSize(new java.awt.Dimension(32767, 10));
        jPnlBottom.setMinimumSize(new java.awt.Dimension(0, 1));
        jPnlBottom.setName("jPnlBottom"); // NOI18N
        jPnlBottom.setPreferredSize(new java.awt.Dimension(400, 10));

        javax.swing.GroupLayout jPnlBottomLayout = new javax.swing.GroupLayout(jPnlBottom);
        jPnlBottom.setLayout(jPnlBottomLayout);
        jPnlBottomLayout.setHorizontalGroup(
            jPnlBottomLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 334, Short.MAX_VALUE)
        );
        jPnlBottomLayout.setVerticalGroup(
            jPnlBottomLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );

        add(jPnlBottom, java.awt.BorderLayout.PAGE_END);

        jPnlRigth.setMaximumSize(new java.awt.Dimension(10, 32767));
        jPnlRigth.setMinimumSize(new java.awt.Dimension(1, 0));
        jPnlRigth.setName("jPnlRigth"); // NOI18N
        jPnlRigth.setPreferredSize(new java.awt.Dimension(10, 190));

        javax.swing.GroupLayout jPnlRigthLayout = new javax.swing.GroupLayout(jPnlRigth);
        jPnlRigth.setLayout(jPnlRigthLayout);
        jPnlRigthLayout.setHorizontalGroup(
            jPnlRigthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );
        jPnlRigthLayout.setVerticalGroup(
            jPnlRigthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 218, Short.MAX_VALUE)
        );

        add(jPnlRigth, java.awt.BorderLayout.LINE_END);

        jPnlLeft.setMaximumSize(new java.awt.Dimension(10, 32767));
        jPnlLeft.setMinimumSize(new java.awt.Dimension(1, 0));
        jPnlLeft.setName("jPnlLeft"); // NOI18N
        jPnlLeft.setPreferredSize(new java.awt.Dimension(10, 190));

        javax.swing.GroupLayout jPnlLeftLayout = new javax.swing.GroupLayout(jPnlLeft);
        jPnlLeft.setLayout(jPnlLeftLayout);
        jPnlLeftLayout.setHorizontalGroup(
            jPnlLeftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );
        jPnlLeftLayout.setVerticalGroup(
            jPnlLeftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 218, Short.MAX_VALUE)
        );

        add(jPnlLeft, java.awt.BorderLayout.LINE_START);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPnlBottom;
    private javax.swing.JPanel jPnlImages;
    private javax.swing.JPanel jPnlLeft;
    private javax.swing.JPanel jPnlRigth;
    private javax.swing.JPanel jPnlTop;
    private javax.swing.JScrollPane jScrollPaneCenter;
    // End of variables declaration//GEN-END:variables

    /**
     * Remove all images from map
     */
    public void clearImages() {
        this.jPnlImages.removeAll();
        this.jPnlImages.revalidate();
        this.clicked = null;
        this.components = null;
    }

    /**
     * Load image list on image map to show.
     *
     * @param images
     */
    public void loadImageList(List<Image> images) {
        this.clearImages();
        this.components = new Vector<JPnlImg>();
        Iterator<Image> it = images.iterator();
        JPnlImg pnl = null;
        int rows = images.size() / 4;
        int mod = images.size() % 4;
        GridLayout gd = (GridLayout) this.jPnlImages.getLayout();
        gd.setColumns((rows > 0) ? 4 : mod);
        gd.setRows(rows + ((mod > 0) ? 1 : 0));
        int i = 0;
        while (it.hasNext()) {
            i++;
            pnl = new JPnlImg(it.next(), this, i);
            this.components.add(pnl);
            this.jPnlImages.add(pnl);
        }
        this.revalidate();
        this.jScrollPaneCenter.getVerticalScrollBar().setUnitIncrement(30);
    }

    /**
     *
     * @param clicked
     */
    public void clickOnImage(JPnlImg clicked) {
        if (this.clicked != null) {
            this.clicked.unclikc();
        }
        this.clicked = clicked;
    }

    /**
     *
     * @param selected
     */
    public void applyCorrelation(JPnlImg selected) {
        if (selected != null) {
            int selectedIndex = this.components.indexOf(selected);
            if (selectedIndex > -1) {
                this.parent.applyCorrelation(this.controller.getAnalysis().getAllSegments().getSegments().elementAt(selectedIndex));
            }
        }
    }

    /**
     *
     */
    protected void clearSelection() {
        if (this.components != null) {
            Iterator<JPnlImg> it = this.components.iterator();
            while (it.hasNext()) {
                it.next().unselect();
            }
        }
    }

    /**
     *
     * @param clicked
     */
    public void select(JPnlImg clicked) {
        if (clicked != null) {
            int selectedIndex = this.components.indexOf(clicked);
            if (selectedIndex > -1) {
                this.controller.getSelectionModel().addSegment(selectedIndex);
            }
        }
    }

    /**
     *
     * @param clicked
     */
    public void unselect(JPnlImg clicked) {
        if (clicked != null) {
            int selectedIndex = this.components.indexOf(clicked);
            if (selectedIndex > -1) {
                this.controller.getSelectionModel().removeSegment(selectedIndex);
            }
        }
    }

    /**
     *
     */
    public void unselectAll() {
        Iterator<JPnlImg> pnls = this.components.iterator();
        while (pnls.hasNext()) {
            pnls.next().unselectPlot();
        }
    }

    public void update(Observable o, Object arg) {
        if (arg == null) {
            this.clearSelection();
            SelectionModel model = (SelectionModel) o;
            if (!model.isSelectionEmpty()) {
                Iterator<SelectionItem> itemsIt = model.getCurrentSegments().iterator();
                while (itemsIt.hasNext()) {
                    SelectionItem selectItem = itemsIt.next();
                    if (selectItem.isSelected()) {
                        this.components.get(selectItem.getIndexSegement()).selectPlot();
                    }
                }
            }
        } else {
            SelectionItem selectItem = (SelectionItem) arg;
            if (this.components.get(selectItem.getIndexSegement()) != null) {
                if (selectItem.isSelected()) {
                    if (!this.components.get(selectItem.getIndexSegement()).isSelected()) {
                        this.components.get(selectItem.getIndexSegement()).selectPlot();
                    }
                } else {
                    if (this.components.get(selectItem.getIndexSegement()).isSelected()) {
                        this.components.get(selectItem.getIndexSegement()).unselectPlot();
                    }
                }
            }
        }
    }
}

